package com.gxwy.zizhanghu.dao;


import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gxwy.zizhanghu.form.ZiZhangHu;
import com.gxwy.upanyonghu.form.UpanYongHu;
import com.gxwy.yonghuziliao.form.YongHuZiLiao;


@Repository
public class ZiZhangHuDaoImpl implements ZiZhangHuDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	public ZiZhangHu findByZhangHuZhangHao(String zhzh) {
		return (ZiZhangHu) sessionFactory.getCurrentSession().createQuery("from ZiZhangHu z where z.zhanghuzhanghao=?").setString(0,zhzh).uniqueResult();
	}

	public void updateyue(Float yue, String zhzh) {
		sessionFactory.getCurrentSession().createQuery("update ZiZhangHu z set z.keyongyue=?,z.yue=? where z.zhanghuzhanghao=?").setFloat(0, yue).setFloat(1,yue).setString(2, zhzh).executeUpdate();
		
	}
	public List<YongHuZiLiao> yonghuziliao(){
	      List<YongHuZiLiao> query= (List<YongHuZiLiao>) sessionFactory.getCurrentSession().createQuery("select s.yinhangzhanghao from YongHuZiLiao s").list();
		  
	      return query;
		
	}
    @SuppressWarnings("unchecked")
	public List<ZiZhangHu> findAll(){
              List<ZiZhangHu> list=(List<ZiZhangHu>) sessionFactory.getCurrentSession().createQuery("from ZiZhangHu").list();
    	return list;
    }


	@SuppressWarnings("unchecked")
	public List<YongHuZiLiao> listContact() {
     
		return sessionFactory.getCurrentSession().createQuery("from YongHuZiLiao")
				.list();
	}

	@SuppressWarnings("unchecked")
	public List<ZiZhangHu> listContactID(String id) {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createQuery
				("from ZiZhangHu where zhanghuzhanghao='"+id+"'").list();
	}

	@SuppressWarnings("unchecked")
	public String add(ZiZhangHu zizhanghu) {
      
	 YongHuZiLiao yonghuziliao=new YongHuZiLiao();
	 List query=	sessionFactory.getCurrentSession().createQuery("from YongHuZiLiao").list();
	 for(int i=0;i<query.size();i++){
		 yonghuziliao=(YongHuZiLiao) query.get(i);
			if(zizhanghu.getZhanghuzhanghao().equals(yonghuziliao.getYinhangzhanghao())){
				zizhanghu.setYonghuziliao(yonghuziliao);
				}
			 }   
	  	     if(zizhanghu.getYonghuziliao()!=null){
	    	 sessionFactory.getCurrentSession().save(zizhanghu);
	    	 return "null";
	     }
		return "null";
	}

	public void update(ZiZhangHu zizhanghu, Integer id) {
		//�存�
	String hql="update ZiZhangHu  set bizhong=?,chaohuibiaozhi=?,chuzhong=?,cunqi=?,keyongyue=?,xucuncunqi=?,yue=?,zhanghumingcheng=?,zhanghuzhanghao=? where id=?";
		
		Query query=(Query) sessionFactory.getCurrentSession().createQuery(hql);
		((org.hibernate.Query) query).setString(0,zizhanghu.getBizhong());
		query.setString(1, zizhanghu.getChaohuibiaozhi());
		query.setString(2, zizhanghu.getChuzhong());
		query.setInteger(3, zizhanghu.getCunqi()) ;
		query.setFloat(4,zizhanghu.getKeyongyue());
		query.setInteger(5,zizhanghu.getXucuncunqi());
		query.setFloat(6,zizhanghu.getYue());
		query.setString(7, zizhanghu.getZhanghumingcheng());
		query.setString(8,zizhanghu.getZhanghuzhanghao());
		query.setInteger(9, id);
		query.executeUpdate();

	}
	public List<YongHuZiLiao>  select(String zhanghuzhanghao){
		String hql="select a.yinhangzhanghao from YongHuZiLiao a where a.yinhangzhanghao like:yinhangzhanghao";

	    Query query=(Query) sessionFactory.getCurrentSession().createSQLQuery(hql);

		query.setParameter("yinhangzhanghao","%"+zhanghuzhanghao+"%",Hibernate.STRING);
		
	
		@SuppressWarnings("unchecked")
		List<YongHuZiLiao> list=query.list();
	
		return list;
	}

	public void Delete(Integer id) {
	
			ZiZhangHu contact = (ZiZhangHu) sessionFactory.getCurrentSession().load(
					ZiZhangHu.class, id);
			if (null != contact) {
				sessionFactory.getCurrentSession().delete(contact);
		
		}
	}
   

}
